﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using Newtonsoft.Json;

namespace WxLib.Common
{
    /// <summary>
    /// 微信AES-128-CBC加密数据的解密
    /// </summary>
    public class WXBizDataCrypt
    {
        /// <summary>
        /// 根据微信小程序平台提供的解密算法解密数据
        /// </summary>
        /// <param name="encryptedData">加密数据</param>
        /// <param name="iv">初始向量</param>
        /// <param name="sessionKey">从服务端获取的SessionKey</param>
        /// <returns></returns>
        public static T Decrypt<T>(string encryptedData, string iv, string sessionKey)
        {
            T result = default(T);
            if (string.IsNullOrEmpty(encryptedData) || string.IsNullOrEmpty(iv) || string.IsNullOrEmpty(sessionKey))
                return result;
            //创建解密器生成工具实例
            AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
            //设置解密器参数
            aes.Mode = CipherMode.CBC;
            aes.BlockSize = 128;
            aes.Padding = PaddingMode.PKCS7;
            //格式化待处理字符串
            byte[] byte_encryptedData = Convert.FromBase64String(encryptedData);
            byte[] byte_iv = Convert.FromBase64String(iv);
            byte[] byte_sessionKey = Convert.FromBase64String(sessionKey);

            aes.IV = byte_iv;
            aes.Key = byte_sessionKey;
            //根据设置好的数据生成解密器实例
            ICryptoTransform transform = aes.CreateDecryptor();

            //解密
            byte[] final = transform.TransformFinalBlock(byte_encryptedData, 0, byte_encryptedData.Length);

            //生成结果
            string json = Encoding.UTF8.GetString(final);

            //反序列化结果，生成用户信息实例
            result = JsonConvert.DeserializeObject<T>(json);

            return result;

        }
    }
}
